<script>
import baseMiXin from "@/views/robotOffice/task/mixin/baseMiXin";
import { parseDate1 } from "@/utils/service";

export default {
  name: "pddDailyData",
  mixins: [baseMiXin],
  data() {
    let _vm = this
    return {
      options: {
        maxQueryTime: [],
        defYesterday: true
      },
      form: {
        account: "",
        password: "",
        queryTimeList: []
      },
      rules: {
        account: [
          { required: true, message: "请输入账号", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" }
        ],
        queryTimeList: [
          { type: 'array',required: true, message: "请选择数据日期", trigger: "blur" },
          {
            validator: (_rule,value,callback) => {
              if (!_vm.options.defYesterday && value.length === 0){
                callback(new Error("请选择数据日期"))
              }else{
                callback();
              }
            },
            trigger: "blur"
          }
        ]
      }
    };
  },
  watch:{
    "options.defYesterday":{
      handler(val) {
        this.defYesterdayChanged()
      },
      immediate: true
    }
  },
  created() {
    // 初始化最大可选日期
    let date = new Date();
    date.setTime(date.getTime() - 3600 * 1000 * 24);
    this.options.maxQueryTime = [date,date];
  },
  methods: {
    initParams({ account = "", password = "", queryTimeList = [] ,defYesterday = true}) {
      this.form.account = account;
      this.form.password = password;
      this.form.queryTimeList = this.getStartAndEndDate(queryTimeList);
      this.options.defYesterday = defYesterday
    },
    checkParam() {
      let r = true
      this.$refs["form"].validate((valid) => {
        r = valid
        return r
      })
      if (!r){
        this.$message.warning("[拼多多-店铺-每日数据] 任务有参数填写错误")
      }
      return r
    },
    getParam() {
      return {
        defYesterday: this.options.defYesterday,
        queryTimeList: this.getDatesBetween(this.form.queryTimeList[0], this.form.queryTimeList[1]),
        account: this.form.account,
        password: this.form.password
      };
    },
    getDatesBetween(startDate, endDate) {
      const dateList = [];
      // 循环遍历每一天，并将日期转换为字符串格式
      for (let currentDate = new Date(startDate); currentDate <= endDate; currentDate.setDate(currentDate.getDate() + 1)) {
        const year = currentDate.getFullYear();
        const month = String(currentDate.getMonth() + 1).padStart(2, '0');
        const day = String(currentDate.getDate()).padStart(2, '0');
        const formattedDate = `${year}-${month}-${day}`;
        dateList.push(formattedDate);
      }
      return dateList;
    },
    getStartAndEndDate(dateStringList) {
      const startDateString = dateStringList[0];
      const endDateString = dateStringList[dateStringList.length - 1];

      const startDate = new Date(startDateString);
      const endDate = new Date(endDateString);

      return [startDate, endDate];
    },
    defYesterdayChanged(){
      this.form.queryTimeList = this.options.defYesterday ? this.options.maxQueryTime : this.form.queryTimeList
      return this.form.queryTimeList
    },
    queryTimeChanged(v) {
      if (v && (v[0] > this.options.maxQueryTime[1] || v[1] > this.options.maxQueryTime[1])) {
        this.form.queryTimeList = this.options.maxQueryTime;
        this.$message.warning("最多只能选择昨天以及之前的数据");
      }
    }
  }
};
</script>

<template>
  <div>
    <el-form :model="form" ref="form" :rules="rules" label-width="60px" size="mini" label-position="left"
             label-suffix=" :">
      <el-form-item label="账号" prop="account" style="display: inline-block">
        <el-input v-model="form.account" placeholder="请输入账号" clearable style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password" style="display: inline-block;margin-left: 50px">
        <el-input v-model="form.password" placeholder="请输入密码" clearable show-password
                  style="width: 300px"></el-input>
      </el-form-item>
      <el-form-item label="数据日期" label-width="90px" prop="queryTimeList">
        <el-radio v-model="options.defYesterday" :label=true>昨日</el-radio>
        <el-radio v-model="options.defYesterday" :label=false>手选</el-radio>
        <el-date-picker
          type="daterange"
          v-if="!options.defYesterday"
          @change="queryTimeChanged"
          v-model="form.queryTimeList"
          placeholder="选择日期"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        >
        </el-date-picker>
      </el-form-item>
    </el-form>
  </div>
</template>

<style scoped>

</style>